package com.kejiguan.ui.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.kejiguan.ui.R;
import com.kejiguan.ui.model.NeItem;
import com.kejiguan.ui.util.CommonDate;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by YYY on 16/7/19.
 */
public class NeItemSelectAdapter extends RecyclerView.Adapter<NeItemSelectVH> implements View.OnClickListener{

    private Context context;
    private List<NeItem> neItems;
    private LayoutInflater layoutInflater;
    private NeItem currentNeItem;
    private View.OnClickListener onClickListener;


    public NeItemSelectAdapter(Context context, List<NeItem> neItems, View.OnClickListener onClickListener)
    {
        this.context=context;
        this.neItems=neItems;
        this.onClickListener=onClickListener;
        layoutInflater=LayoutInflater.from(context);
    }


    @Override
    public NeItemSelectVH onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView=layoutInflater.inflate(R.layout.activity_select_ne_item_layout,null);
        return new NeItemSelectVH(itemView);
    }

    @Override
    public void onBindViewHolder(NeItemSelectVH holder, int position) {

        NeItem neItem=neItems.get(position);
        holder.neIdView.setText(neItem.getId());
        holder.neNameView.setText(TextUtils.isEmpty(neItem.getNeName())?"":neItem.getNeName());
        holder.neIpView.setText(TextUtils.isEmpty(neItem.getNeIp())?"":neItem.getNeIp());

        if (neItem.isSelectForExhibition())
        {
            holder.selectStateView.setImageResource(R.mipmap.kjg_menu_checked);
        }else
        {
            holder.selectStateView.setImageResource(R.mipmap.kjg_menu_uncheck);
        }

        holder.selectStateView.setTag(R.id.recyclerview_item_position,position);
        holder.selectStateView.setOnClickListener(this);
        if (null!=onClickListener)
        {
            holder.neItemLayout.setTag(R.id.recyclerview_item_position,position);
            holder.neItemLayout.setOnClickListener(onClickListener);
        }

    }

    @Override
    public int getItemCount() {

        if (null==neItems)
        {
            return 0;
        }else
        {
            return neItems.size();
        }

    }

    @Override
    public void onClick(View v) {

        int position= (int) v.getTag(R.id.recyclerview_item_position);

        switch (v.getId())
        {
            case R.id.select_state:
                currentNeItem=neItems.get(position);
                if (currentNeItem.isSelectForExhibition())
                {
                    currentNeItem.setSelectForExhibition(false);
                }else
                {
                    currentNeItem.setSelectForExhibition(true);
                }
                notifyDataSetChanged();

                break;
        }

    }


    public void setNeItems(List<NeItem> neItems) {
        this.neItems = neItems;
        currentNeItem=null;
        notifyDataSetChanged();
    }

    public NeItem getCurrentNeItem() {
        return currentNeItem;
    }

    public String getSelectNeIds()
    {
        String selectIds=null;
        StringBuffer ids=new StringBuffer();
        if (null!=neItems)
        {
            for (NeItem neItem:neItems)
            {
                if (neItem.isSelectForExhibition())
                {
                    ids.append(neItem.getId()).append(",");
                }
            }
        }
        selectIds=ids.toString();
        return selectIds;
    }


    public NeItem getNeItemByPosition(int position)
    {

        if (null!=neItems&&position<neItems.size())
        {
            return neItems.get(position);
        }else
        {
            return null;
        }
    }

    public void setNeItem(int position,NeItem neItem)
    {

        if (null!=neItem&&position<neItems.size())
        {
            neItems.set(position,neItem);
            notifyDataSetChanged();
        }
    }
    public void addNeItem(NeItem neItem)
    {
        if (null==neItems)
        {
            neItems=new ArrayList<>(0);
        }
        neItems.add(0,neItem);
        notifyDataSetChanged();
    }


}
